package com.yl.ydk.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.presenter.contract.CompletedUserInfoActivityContract
import com.yl.ydk.presenter.impl.CompletedUserInfoActivityImpl
import com.yl.ydk.utils.DialogUtils
import com.yl.ydk.utils.GlideUtils
import com.blankj.utilcode.utils.StringUtils
import com.blankj.utilcode.utils.ToastUtils
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import kotlinx.android.synthetic.main.activity_completed_user_info.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class CompletedUserInfoActivity : BaseActivity(), View.OnClickListener,
    CompletedUserInfoActivityContract.View {

    private val mPresenter = CompletedUserInfoActivityImpl()

    private var imgPath: String? = null
    private var photo: String? = null
    private var sex: String? = null
    private var height: String? = null
    private var weight: String? = null
    private var birthday: String? = null

    override fun getLayoutId(): Int = R.layout.activity_completed_user_info

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {
        ivHead.setOnClickListener(this)
        rlAge.setOnClickListener(this)
        rlSex.setOnClickListener(this)
        rlWeight.setOnClickListener(this)
        rlHeight.setOnClickListener(this)
        btnPost.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivHead -> {
                PictureSelector
                    .create(this, PictureSelector.SELECT_REQUEST_CODE)
                    .selectPicture(false)
            }
            R.id.rlSex -> {
                DialogUtils.showCheckSex(this, {
                    DialogUtils.hideDialog()
                    tvSex.text = "男"
                    sex = 1.toString()
                }, {
                    DialogUtils.hideDialog()
                    tvSex.text = "女"
                    sex = 2.toString()
                })
            }

            R.id.rlAge -> {
                DialogUtils.showCheckAge(this, object : DialogUtils.OnBtnOkListener {
                    override fun OnBtnOkListener(item: String?) {
                        tvAge.text = item
                        birthday = item
                    }
                })
            }

            R.id.rlWeight -> {
                DialogUtils.showCheckWeight(this, 60f, object : DialogUtils.OnBtnOkListener {
                    @SuppressLint("SetTextI18n")
                    override fun OnBtnOkListener(item: String?) {
                        tvWeight.text = "${item}kg"
                        weight = item
                    }
                })
            }

            R.id.rlHeight -> {
                DialogUtils.showCheckWeight(this, 170f, object : DialogUtils.OnBtnOkListener {
                    @SuppressLint("SetTextI18n")
                    override fun OnBtnOkListener(item: String?) {
                        tvHeight.text = "${item}cm"
                        height = item
                    }
                })
            }

            R.id.btnPost -> {
                if (StringUtils.isEmpty(etName.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写姓名")
                    return
                }
                if (StringUtils.isEmpty(tvSex.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写性别")
                    return
                }
                if (StringUtils.isEmpty(tvAge.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写年龄")
                    return
                }
                if (StringUtils.isEmpty(tvWeight.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写体重")
                    return
                }
                if (StringUtils.isEmpty(tvHeight.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写身高")
                    return
                }
                if (StringUtils.isEmpty(etFamil.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请填写家庭关系")
                    return
                }
                if (StringUtils.isEmpty(photo)) {
                    ToastUtils.showLongToastSafe("请选择头像")
                    return
                }

                mPresenter.getSaveUser(
                    this,
                    "phone", "15118183011",
                    "name", etName.text.toString().trim(),
                    "photo", photo,
                    "sex", sex,
                    "height", height,
                    "weight", weight,
                    "birthday", birthday
                )
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            val pictureBean: PictureBean? = data?.getParcelableExtra(PictureSelector.PICTURE_RESULT)
            pictureBean?.let {
                imgPath = it.path
                val requestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
                val file = File(it.path)
                val filename = file.name
                val body = RequestBody.create(MediaType.parse("image/*"), file)
                requestBody.addFormDataPart("file[]", filename, body)
                GlideUtils.loadRound(this@CompletedUserInfoActivity, imgPath, ivHead)
                mPresenter.getBase64Upload(this@CompletedUserInfoActivity, requestBody.build())
            }
        }
    }

    override fun showError(message: String?) {
        ToastUtils.showLongToastSafe(message)
    }

    override fun getIvHead(): ImageView? = ivHead

    override fun getActivity(): CompletedUserInfoActivity = this

    override fun getPhoto(photo: String) {
        this.photo = photo
    }

}